package com.hdcx.Minfresh.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.bean.AdressWrapper;
import com.hdcx.Minfresh.bean.UserInfoBean;
import com.hdcx.Minfresh.url.MyUrl;
import com.hdcx.Minfresh.util.AppUtil;
import com.hdcx.Minfresh.util.ShpfUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/11/18.
 */

public class AddAddressActivity extends BaseActivity {

    EditText ed_name,ed_phone,ed_address;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addaddress);
        initView();
        initData();
    }

    private void initView() {
        ed_name = (EditText) findViewById(R.id.ed_name);
        ed_phone = (EditText) findViewById(R.id.ed_phone);
        UserInfoBean user = ShpfUtil.getObject(ShpfUtil.LOGIN);
        ed_phone.setText(user.getUserinfo().getMobile());
        ed_address = (EditText) findViewById(R.id.ed_address);
        findViewById(R.id.bt_ok).setOnClickListener(this);
        findViewById(R.id.tv_left).setOnClickListener(this);
        TextView title = (TextView) findViewById(R.id.tv_title);
        title.setText("新增收货地址");
        TextView right = (TextView) findViewById(R.id.tv_right);
        //right.setText("帮助");
        right.setOnClickListener(this);

    }
    private void initData() {

    }
   /* uid   //用户id  20
    name 	//收货人
    mobile	//收货人手机号码
    address	//收货人地址*/
    private void post_main() {
        if(AppUtil.need_to_login(this)){
            return;
        }
        String name = ed_name.getText().toString().trim();
        if(name==null||name.equals("")){
            Toast.makeText(AddAddressActivity.this,"请输入姓名！",Toast.LENGTH_SHORT).show();
            return;
        }
        String phone = ed_phone.getText().toString().trim();
        if(phone==null||phone.equals("")){
            Toast.makeText(AddAddressActivity.this,"请输入电话！",Toast.LENGTH_SHORT).show();
            return;
        }
        String address = ed_address.getText().toString().trim();
        if(address==null||address.equals("")){
            Toast.makeText(AddAddressActivity.this,"请输入地址！",Toast.LENGTH_SHORT).show();
            return;
        }
        UserInfoBean user =  ShpfUtil.getObject(ShpfUtil.LOGIN);
        JSONObject json = new JSONObject();
        try {
            json.put("uid",user.getUserinfo().getId());
            json.put("name",name);
            json.put("mobile",phone);
            json.put("address",address);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.postString()
                .url(MyUrl.SETADDRESS)
                .content(json.toString())
                .build()
                .execute(new Callback<AdressWrapper>() {
                    @Override
                    public AdressWrapper parseNetworkResponse(Response response) throws IOException {
                        String json = response.body().string();
                        return new Gson().fromJson(json, AdressWrapper.class);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                    }

                    @Override
                    public void onResponse(AdressWrapper response) {
                        Toast.makeText(AddAddressActivity.this,response.getInfo(),Toast.LENGTH_SHORT).show();
                        if (response.getStatus() == 1) {
                            setResult(RESULT_OK);
                            finish();
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_ok:
                post_main();
                break;
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_right:
                break;
        }
    }


}
